package org.ldh.web.servlet;

import org.ldh.domain.User;
import org.ldh.service.UserService;
import org.ldh.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/userServlet.action")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------UserServlet-------");
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取请求参数
        String action = request.getParameter("action");
        System.out.println("action:"+action);
        //调用service层
        UserService userService = new UserServiceImpl();

        //判断请求参数
        if(action != null && action.equals("list")){
            //查询
            List<User> users = new ArrayList<User>();
            users = userService.findAll();
            //查询所有宿舍楼，将users存储到request中，跳转到宿舍楼列表页
            request.setAttribute("users",users);
            request.setAttribute("mainRight","/WEB-INF/pages/SystemManagerList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("add")){
            //添加用户
            //跳转到添加页面
            request.setAttribute("mainRight","/WEB-INF/pages/SystemMangerAdd.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("save")){
            //保存用户
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            //先判断用户名是否重复
            User user = userService.findByNameAndPass(username, password);
            if(user == null) {
                //说明数据库中不存在用户输入的信息
                //封装实体类
                User user2 = new User();
                user2.setUsername(username);
                user2.setPassword(password);
                //调用保存方法，将数据保存带数据库中
                userService.save(user2);
                //保存成功后，重新查询数据库
                List<User> users = new ArrayList<User>();
                users = userService.findAll();
                //重新存储用户信息到request域
                request.setAttribute("users",users);
                //跳转到列表页
                request.setAttribute("mainRight","/WEB-INF/pages/SystemManagerList.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }else{
                //说明数据库中已存在该用户
                //存储错误信息到request域
                request.setAttribute("error","该用户名已存在，请重新输入！");
                //跳转到添加页面
                request.setAttribute("mainRight","/WEB-INF/pages/SystemMangerAdd.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }
        }else if(action != null && action.equals("update")){
            //修改
            //获取请求参数
            String id = request.getParameter("id");
            //根据id查询数据库
            User user = userService.findById(id);
            //获取到该用户的username和password
            String username = user.getUsername();
            String password = user.getPassword();
            //存储到request中，在前端显示
            request.setAttribute("id",id);
            request.setAttribute("username",username);
            request.setAttribute("password",password);
            //跳转到修改页面
            request.setAttribute("mainRight","/WEB-INF/pages/SystemMangerUpdate.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("reUpdate")){
            //更新
            //获取请求参数
            String id = request.getParameter("id");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            //封装实体类
            User user = new User();
            user.setId(Integer.parseInt(id));
            user.setUsername(username);
            user.setPassword(password);
            //调用service层的修改方法
            userService.update(user);
            //修改完成之后，重新查询数据库
            List<User> users = new ArrayList<User>();
            users = userService.findAll();
            //重新存储用户信息到request域
            request.setAttribute("users",users);
            //跳转到列表页
            request.setAttribute("mainRight","/WEB-INF/pages/SystemManagerList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);

        }else if(action != null && action.equals("delete")){
            //删除
            //获取请求参数
            String id = request.getParameter("id");
            //通过id删除用户
            userService.deleteById(id);
            //删除成功后，重新查询数据库
            List<User> users = new ArrayList<User>();
            users = userService.findAll();
            //重新存储用户信息到request域
            request.setAttribute("users",users);
            //跳转到列表页
            request.setAttribute("mainRight","/WEB-INF/pages/SystemManagerList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("index")){
            //跳转到首页
            request.setAttribute("mainRight","/WEB-INF/pages/blank.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
